package com.chuangsi.project.admin.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangsi.common.utils.MyStringUtils;
import com.chuangsi.project.admin.domain.TLog;
import com.chuangsi.project.admin.dto.admin.log.LogPageQueryRes;
import com.chuangsi.project.admin.mapper.TLogMapper;
import com.chuangsi.project.admin.service.ITLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author ocean
 * @since 2020-02-06
 */
@Service
public class TLogServiceImpl extends ServiceImpl<TLogMapper, TLog> implements ITLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(TLog log) {
        //创建本月日志表（没有就创建，有就不创建）
        String tableName = "t_log_"+ LocalDate.now().toString().substring(0,7).replace("-","");
//        baseMapper.ifExisted(tableName);
//        baseMapper.insert(MyStringUtils.generatePrimaryKey(),tableName,log.getActionurl(),log.getCommite(),log.getContent(),log.getIp(),log.getMethods(),log.getModule(),log.getUserName(),log.getMsg());
    }

    @Override
    public IPage<LogPageQueryRes> logPageQuery(Page<LogPageQueryRes> logPageQueryResPage, String userName, LocalDateTime startTime, LocalDateTime endTime) {
        String tableName = "t_log_"+LocalDate.now().toString().substring(0,7).replace("-","");
        if (startTime != null && endTime != null) {
            tableName= "t_log_"+startTime.toString().substring(0,7).replace("-","");
        }
        baseMapper.ifExisted(tableName);
        return baseMapper.logPageQuery(logPageQueryResPage,userName,startTime,endTime,tableName);
    }
}
